<?php
class Default_RestController extends Ac_Rest_Controller
{

    /**
        return one random listing
     */
    public function indexAction() 
    {
        $id   = $this->_request->getParam('id');
        if($id == 'attribute') {
            return $this->attributeAction();
        }
    }

    public function attributeAction()
    {
        $view = Zend_Registry::get('Zend_View');
        if (!empty($_GET['type']) && $_GET['type'] == 'district') {
            $res = array();
            $areas = Model_Location::getAreaSearch();
            foreach($areas as $k => $area) {
                if(empty($k)) {continue;}
                $districts = Model_Location::getDistrictSearch($k);
                $tmp = array();
                foreach($districts as $k2 => $district) {
                    if(!empty($k2)) {
                        //$res[$area][] = array('id' => (string) $k2, 'name' => $district);
                        $tmp[] = array('id' => (string) $k2, 
                                       'name_en' => $view->t($district),
                                       'name_tc' => $view->t($district, 'tc'));
                    }
                }
                $res[] = array('name_en' => $view->t($area), 
                               'name_tc' => $view->t($area, 'tc'),
                               'values' => $tmp);
            }
            echo json_encode($res);
            exit;
        } elseif (!empty($_GET['type']) && $_GET['type'] == 'car-make') {
            $list = Model_Listing_Car::getMake();
            $res = array();
            foreach($list as $k => $make) {
                if(!empty($k)) {
                    $res[] = array('id' => (string) $k, 'name' => $make);
                }
            }
            echo json_encode($res); 
            exit;
        } elseif (!empty($_GET['type']) && $_GET['type'] == 'car-model' && !empty($_GET['id'])) {
            $list = Model_Listing_Car::getModel($_GET['id']);
            $res = array();
            foreach($list as $k => $model) {
                if(!empty($k)) {
                    $res[] = array('id' => (string) $k, 'name' => $model);
                }
            }
            echo json_encode($res);
            exit;
        } elseif (!empty($_GET['type']) && $_GET['type'] == 'boat-make' && !empty($_GET['id'])) {
            $list = Model_Listing_Boat::getBoats($_GET['id']);
            $res = array();
            foreach($list as $k => $make) {
                if(!empty($k)) {
                    $res[] = array('id' => (string) $k, 'name' => $make);
                }
            }
            echo json_encode($res);
            exit;
        } elseif (!empty($_GET['type']) && $_GET['type'] == 'boattype') {
            $list = Model_Listing_Boat::getBoatType();
            $res = array();
            foreach($list as $k => $type) {
                if(!empty($k)) {
                    $res[] = array('id' => (string) $k, 'name' => $type);
                }
            }
            echo json_encode($res);
            exit;
        } elseif (!empty($_GET['type']) && $_GET['type'] == 'boatcategory' && !empty($_GET['id'])) {
            $list = Model_Listing_Boat::getBoatCategory($_GET['id']);
            $res = array();
            foreach($list as $k => $type) {
                if(!empty($k)) {                        
                    $res[] = array('id' => (string) $k, 'name' => $type);
                }
            }       
            echo json_encode($res);             
            exit; 
        } elseif (!empty($_GET['type']) && $_GET['type'] == 'motorcycle-make') {
            $list = Model_Listing_Motorcycle::getMake();
            $res = array();
            foreach($list as $k => $make) {
                if(!empty($k)) {
                    $res[] = array('id' => (string) $k, 'name' => $make);
                }
            }
            echo json_encode($res);
            exit;
        } elseif (!empty($_GET['type']) && $_GET['type'] == 'motorcycle-model' && !empty($_GET['id'])) {
            $list = Model_Listing_Motorcycle::getModel($_GET['id']);
            $res = array();
            foreach($list as $k => $model) {
                if(!empty($k)) {
                    $res[] = array('id' => (string) $k, 'name' => $model);
                }
            }
            echo json_encode($res);
            exit;

       } 
    }

    /**
        return one specific listing
     */
    public function getAction() 
    {
    }

    public function postAction() 
    {
    }

}
